package cn.edu.ujs.coor.common.exception;

import cn.edu.ujs.coor.response.JsonResponse;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 公共异常类处理
 *
 * @author 柳曾雄
 * @date 2018-07-16
 */
@ControllerAdvice
public class ExceptionController {

    /**
     * 自定义异常
     * @param e
     * @return
     */
    @ResponseBody
    @ExceptionHandler(CoorException.class)
    public JsonResponse coor (CoorException e) {
        return JsonResponse.fail(e.getCode(), e.getMessage());
    }

    /**
     * 参数校验异常
     * @param e
     * @return
     */
    @ResponseBody
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public JsonResponse argument (MethodArgumentNotValidException e) {
        return JsonResponse.fail("er_000002", e.getBindingResult().getAllErrors().get(0).getDefaultMessage());
    }

    /**
     * 其它系统异常
     * @param e
     * @return
     */
    @ResponseBody
    @ExceptionHandler(Exception.class)
    public JsonResponse exception (Exception e) {
        e.printStackTrace();
        return JsonResponse.fail("er_000002", "系统错误");
    }
}
